<template>
  <div class="modal" ref="modal">
    <div class="modal-content" ref="content">
      <div class="close">
        <img @click="toggleDialog" src="../../assets/img/modal_close.svg" alt="">
      </div>
      
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    option: {}
  },
  data() {
    return {

    }
  },
  mounted() {
    if (this.option.width) {
      this.$refs.content.style.width = this.option.width
    }
    if (this.option.height) {
      this.$refs.content.style.height = this.option.height
    }

    // window.addEventListener('click', (e) => {
    //   if (e.target === this.$refs.modal) {
    //     this.toggleDialog()
    //   }
    // })
  },
  methods: {
    toggleDialog() {
      this.$refs.modal.classList.toggle('show-modal')
    },
  }
}
</script>

<style lang="less" scoped>
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: 99;
  top: 0;
  left: 0;
  background: rgba(0,0,0,0.5);
  visibility: hidden;
  transform: scale(1.1);
  opacity: 0;
  transition: all 0.2s ease;

  .modal-content {
    background: #fff;
    border-radius: 8px;
    position: absolute;
    overflow: hidden;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    position: relative;

    .close {
      position: absolute;
      right: 20px;
      top: 16px;
      width: 24px;
      cursor: pointer;
    }
    .close:hover {
      background: #ebe2e2;
    }
  }
}

.show-modal {
  visibility: visible;
  opacity: 1;
  transform: scale(1);
  transition: all 0.2s ease;
}
</style>